package com.fly.msg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.fly.msg.Constant.P2P_ADDRESS_SUFFIX;
import static com.fly.msg.Constant.TOPIC_MESSAGE_ADDRESS;

/**
 * @author frinder
 * @date 2018/6/1
 * @Description: ${todo}
 */
@Service
public class WebSocketService {

    @Autowired
    private SimpMessagingTemplate template;

    /**
     * 推送消息到前端
     *
     * @param result
     */
    public void send(WiselyResponse result) {
        template.convertAndSend(TOPIC_MESSAGE_ADDRESS, result);
    }

    /**
     * 推送消息到指定 user
     *
     * @param users
     * @param result
     */
    public void send(List<String> users, WiselyResponse result) {
        users.stream().forEach(user -> template.convertAndSendToUser(user, P2P_ADDRESS_SUFFIX, result));
    }
}
